// component/index/sku/sku.js
const app = getApp();
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
      "gifts":{type:Array},
		 "key":{type:String},
		 "myinfo":{type:Object}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
	
	},
//   onShow: function () {
// 		this.myinfos();
	
// },

	lifetimes:{
	   attached() {


		   // app.func.getPromise('/list/home/0?length=20')
	   }
	},

	/**
	 * 组件的方法列表
	 */
	
	methods: {
		sku:function(){
			app.func.getPromise('/list/zone')
				.then(([code, res]) => {
					console.log(res.data)
					this.setData({gifts: res.data});
				})
		},
		productdetail: function (e) {
			var id = e.currentTarget.dataset.id;
			wx.navigateTo({
				url: '/pages/index/productdetail/productdetail?id=' + id
			})
		},
		jumpshop: function () {
			wx.switchTab({
				url: '/pages/index/categorysku/categorysku'
			})
		},
		addChoose:function (e) {
			let id = e.currentTarget.dataset.id;
			app.func.getPromise('/addcart/'+id)
				.then(([code, res]) => {
					console.log(res)
					if(code == 200){
						app.func.toastPromise('成功加入优选', 'none',  500)
							.then(()=>{
								this.sku();
							})
					}
				})
		},
		clearChoose:function (e) {
			let id = e.currentTarget.dataset.id;
			app.func.getPromise('/delcart/'+id)
				.then(([code, res]) => {
					if(code == 200){
						app.func.toastPromise('已移除优选','none',  500)
							.then(()=>{
								this.sku();
							})
					}
				})
		},

		login: function () {
			wx.switchTab({
				url: '/pages/about/about'
			})
		},

		attestation:function(){
		   wx.navigateTo({
			   url: '/pages/auth/preattestation/preattestation'
		   })
		},
		
		//购买
		apply:function (e) {
			var id = e.currentTarget.dataset.id;
			//旧跳转
			// wx.navigateTo({
			// 	url:'/pages/gifts/pages/order/order?id=' + id
			// })
			wx.navigateTo({
				url: '/pages/cartshop/payif/payif?skuid=' + id
		})
		},


		addcart:function(e){
			let cart = this.data.gifts.sku;
			console.log('111')
			console.log(this.data.gifts)
				let id = e.currentTarget.dataset.id;
						app.func.postPromise('/addcartv2',
										{
												pro_id:id,
												count:1
										})  .then(([code, res]) => {
												console.log(res);
												if(code == 200){
														// wx.showToast({
														// 	title: '加入成功',
														// 	icon:'success',
														// 	mask:true,
														// 	duration:1000
		
														// })
	
														app.func.toastPromise('加入购物车成功','none',  500)
														.then(()=>{
															this.sku();
														})
														this.setData({
															noaddcart:false
														})
												}else{
														wx.showToast({
															title:res.message,
															icon:'none',
															mask:true
														})
												}
										})
		
		},
		
	
	}
})
